package stream;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Example {
    public static void main(String[] args) {
        // 获取一班中学生的平均年龄
        System.out.println(getClassList().stream()
        .filter(e  -> e.getName().equals("一班"))
        .flatMap(e -> e.getStudents().stream())
        .mapToInt(Student::getAge)
        .average()
        .orElse(0));

        // 找出所有班级中的学生中的最高分数
        System.out.println(getClassList().stream()
        .flatMap(e -> e.getStudents().stream())
        .mapToInt(Student::getGrade)
        .max()
        .orElse(Integer.MAX_VALUE));

        // 获取所有班级中的学生的数量
        System.out.println(getClassList().stream()
        .flatMap(c -> c.getStudents().stream())
        .count());

        // 获取所有班级中的学生的数量 解法2
        System.out.println(getClassList().stream()
        .mapToInt(c -> c.getStudents().size())
        .sum());

        // 获取所有学生中不同年龄的男女数量 1-男 0-女;
        System.out.println(getClassList().stream()
        .flatMap(c -> c.getStudents().stream())
        .collect(Collectors.groupingBy(Student::getAge,Collectors.groupingBy(Student::getGender,Collectors.counting()))));
    
        // 统计每个班级的男女数量
        System.out.println(getClassList().stream()
        .collect(Collectors.toMap(Class::getName, 
        c -> c.getStudents().stream()
        .collect(Collectors.groupingBy(Student::getGender, Collectors.counting())))));

        // 找出所有班级中成绩前十的学生
        System.out.println(getClassList().stream()
        .flatMap(c -> c.getStudents().stream())
        .sorted(Comparator.comparing(Student::getGrade).reversed())
        .limit(10)
        .collect(Collectors.toList()));

        // 找出每个班级成绩前3名的学生
        System.out.println(getClassList().stream()
        .collect(Collectors.toMap(Class::getName, 
        c -> c.getStudents().stream()
        .sorted(Comparator.comparing(Student::getGrade).reversed())
        .collect(Collectors.toList()))));
        
        // 找出所有班级中包含的年龄
        getClassList().stream()
        .flatMap(c -> c.getStudents().stream())
        .map(Student::getAge)
        .distinct()
        .forEach(System.out::print);

        // 过滤出张三
        getClassList().stream()
        .filter(c -> c.getName().equals("1B"))
        .forEach(c -> System.out.println(c.getName()));
}


        public static List<Class> getClassList() {
        List<Student> students1 = Arrays.asList(
                new Student("张三", 18, "1X", 456),
                new Student("李四", 17, "0X", 432),
                new Student("王五", 16, "1X", 368),
                new Student("小红", 18, "0X", 511),
                new Student("小明", 17, "1X", 517)
        );

        List<Student> students2 = Arrays.asList(
                new Student("小丽", 16, "0X", 554),
                new Student("刘六", 18, "1X", 587),
                new Student("陈七", 17, "1X", 502),
                new Student("赵八", 16, "1X", 498)
        );

        List<Student> students3 = Arrays.asList(
                new Student("林九", 18, "0X", 356),
                new Student("郑十", 17, "1X", 411),
                new Student("孙十一", 16, "1X", 435),
                new Student("吴十二", 18, "0X", 389),
                new Student("朱十三", 17, "0X", 490),
                new Student("许十四", 16, "1X", 543)
        );

        List<Student> students4 = Arrays.asList(
                new Student("何十五", 18, "0X", 612),
                new Student("胡十六", 17, "1X", 698) ,
                new Student("王十七", 16, "0X", 687),
                new Student("黄十八", 18, "1X", 665),
                new Student("马十九", 17, "1X", 701),
                new Student("周二十", 16, "0X", 711),
                new Student("郭二一", 18, "1X", 689)
        );

        List<Student> students5 = Arrays.asList(
                new Student("林二二", 17, "1X", 567),
                new Student("吴二三", 16, "1X", 587),
                new Student("谢二四", 18, "0X", 687),
                new Student("胡二五", 17, "0X", 598),
                new Student("何二六", 16, "1X", 654),
                new Student("王二七", 15, "0X", 512),
                new Student("徐二八", 17, "1X", 633),
                new Student("刘二九", 16, "0X", 632)
        );

        return Arrays.asList(
                new Class("1B",students1),
                new Class("2B",students2),
                new Class("3B",students3),
                new Class("4B",students4),
                new Class("5B",students5)
        );
    }
}
